
from autoTest_brtcApi.core.usercenter_apiUtils import userApiUtil
from autoTest_brtcApi.utils.write import write_yaml
from autoTest_brtcApi.utils.read import readFile

# 请求头
headers = {"Content-Type": "application/json",
           "Accept": "application/json"}
### 在该模块的方法里面，构造请求头和参数
class UserApi():
    def reg_api(self,username,password,phone):
        # 注册
        # 请求参数
        params = {
            "user_name":username,
            "passwd":password,
            "phone":phone
        }
        # 接收接口返回的数据
        res_reg = userApiUtil.reg_utils(headers=headers,json=params)
        return res_reg

    def log_api(self,password,phone):
        # 登录
        # 请求参数
        params = {
            "passwd": password,
            "phone": phone
        }
        # 接收接口返回的数据
        res_log = userApiUtil.log_utils(headers=headers, json=params)
        if res_log.json()['code'] == 0:
            ## 把动态变化的userId和token值存放到yaml文件中
            userId = res_log.json()["data"]['userId']
            token = res_log.json()["data"]['token']
            content = {"userId":str(userId),
                        "token": token}
            write_yaml("tmp.yaml",content)
        return res_log

    def modifyUserNick(self):
        # 读取tmp.yaml，获取userId和token
        response = readFile("tmp.yaml")
        # 请求头
        headers_modifyUserNick = {"userId":str(response["userId"]),
                                  "token":response["token"]}
        # 请求参数
        params = {
            "nickName": "lcx66666"
        }
        # 接收接口返回的数据
        res_modU = userApiUtil.modifyUserNick_utils(headers=headers_modifyUserNick, data=params)
        return res_modU

### 实例化 UserApi
userApi = UserApi()